
var orderId="";

function getData() {
	var APP_ID = 'c4w6bVvWfh8xxcmkBE1G0gIt-gzGzoHsz';
	var APP_KEY = 'JBikg4Ksk76zcTi5BKYWvTJF';
	AV.init({
		appId: APP_ID,
		appKey: APP_KEY
	});
	var query = new AV.Query('ELM_shop');
	query.get(href).then(function(todo) {
		var jop = {
			id: todo.id,
			shopname: todo.attributes.shopname,
			shophours: todo.attributes.shophours,
			startprice: todo.attributes.startprice,
			classify: todo.attributes.classify,
			adress: todo.attributes.adress,
			attr1: todo.attributes.attr1,
			attr2: todo.attributes.attr2,
			attr3: todo.attributes.attr3,
			grade: todo.attributes.grade,
			notice: todo.attributes.notice,
			salenum: todo.attributes.salenum,
			sendprice: todo.attributes.sendprice,
			shophours: todo.attributes.shophours,
			shopimg: todo.attributes.shopimg,
		}
		newvue.oneshop = jop;
		newvue.productclassify = eval(todo.attributes.productclassify);
		newvue.htm = newvue.productclassify[0]
		newvue.price = newvue.oneshop.startprice
		getProductList();
	}, function(error) {
		// 异常处理
	});

	var querys = new AV.Query('ELM_product');
	querys.contains('shopid', href);
	querys.find().then(function(todoo) {
		for(var i = 0; i < todoo.length; i++) {
			var jops = {
				id: todoo[i].id,
				productname: todoo[i].attributes.productname,
				classify: todoo[i].attributes.classify,
				grade: todoo[i].attributes.grade,
				productimg: todoo[i].attributes.productimg,
				price: todoo[i].attributes.price,
				salenum: todoo[i].attributes.salenum,
				shopid: todoo[i].attributes.shopid,
				remark: todoo[i].attributes.remark,
			}
			newvue.oneproduct.push(jops);

		}
		//		console.log(newvue.oneproduct)
	}, function(error) {
		// 异常处理
	});

}

function getProductList() {
	var queryss = new AV.Query('ELM_product');
	for(var i = 0; i < newvue.productclassify.length; i++) {
		queryss.contains('classify', newvue.productclassify[i]);
		queryss.contains('shopid', href);
		queryss.find().then(function(todooo) {
			for(var h = 0; h < todooo.length; h++) {
				var jops = {
					id: todooo[h].id,
					productname: todooo[h].attributes.productname,
					classify: todooo[h].attributes.classify,
					grade: todooo[h].attributes.grade,
					productimg: todooo[h].attributes.productimg,
					price: todooo[h].attributes.price,
					salenum: todooo[h].attributes.salenum,
					shopid: todooo[h].attributes.shopid,
					remark: todooo[h].attributes.remark,
					num: "0"
				}
				newvue.oneproducts.push(jops);
			}
		}, function(error) {
			// 异常处理
		});
	}

}


function signIn() {
	var currentUser = AV.User.current();
	if(currentUser) {
		addorder()
	} else {
		location.href = "../Sign in/signin.html"
	}
}
var href = jiLu(location.search, "shopId");

function jiLu(url, name) {
	c_start = url.indexOf(name + "=") //RetUrl= //1
	if(c_start != -1) {
		c_start = c_start + name.length + 1 //8
		c_end = url.indexOf("&", c_start) //16
		if(c_end == -1) c_end = url.length
		return unescape(url.substring(c_start, c_end))
	}
	return ""
}
function addorder() {
	var products=newvue.order
	var currentUser = AV.User.current();
	var Product = AV.Object.extend('ELM_order');
	var product = new Product();
	product.set('shop_name', newvue.oneshop.shopname);
	product.set('shopimg', newvue.oneshop.shopimg);
	product.set('products_data', products);
	product.set('username', currentUser.attributes.username.toString());
	product.set('totle',newvue.total.toString());
    product.set('shop_sendprice', newvue.oneshop.sendprice);
	product.set('if_pay', false);
	product.save().then(function(ret) {
		orderId = ret.id;
		location.href = "../shoporder/shoporder.html?orderId="+orderId
		console.log(orderId);
	}, function(error) {
	});
}
var newvue = new Vue({
	el: "body",
	data: {
		oneshop: "",
		oneproduct: [],
		productclassify: "",
		oneproducts: [],
		total: 0,
		order: [],
		htm: "",
		price: "",
		shows:false,
		isa:false,
		isb:false
	},
	methods: {
		show: function(index) {
			this.htm = this.productclassify[index]
		},
		add: function(index) {
			this.oneproducts[index].num = parseInt(this.oneproducts[index].num) + 1
			var ad = true;
			if(this.order.length == 0) {
				var objs = {
					id: this.oneproducts[index].id,
					num: this.oneproducts[index].num,
					price: this.oneproducts[index].price,
					name: this.oneproducts[index].productname,
				}
				this.order.push(objs)
			}
			for(var i = 0; i < this.order.length; i++) {
				if(this.order[i].id == this.oneproducts[index].id) {
					this.order[i].num = this.oneproducts[index].num
					ad = false;
				}
			}
			if(ad) {
				var obj = {
					id: this.oneproducts[index].id,
					num: this.oneproducts[index].num,
					price: this.oneproducts[index].price,
					name: this.oneproducts[index].productname,
				}
				this.order.push(obj)
			}
			console.log(this.order)
			this.totals();

		},
		reduce: function(index) {
			this.oneproducts[index].num = parseInt(this.oneproducts[index].num) - 1
			for(var i = 0; i < this.order.length; i++) {
				if(this.order[i].id == this.oneproducts[index].id) {
					this.order[i].num = this.oneproducts[index].num
				}
				if(this.order[i].num == 0) {
						this.order.splice(i, 1)
					}
			}
			console.log(this.order)
			this.totals();
		},
		totals: function() {
			this.total = 0
			for(var i = 0; i < this.order.length; i++) {
				this.total =(this.order[i].num * this.order[i].price +this.total).toFixed(2) 
				this.price = this.oneshop.startprice - this.total
			}
		},
		singIn: function() {
			signIn()
		},
		back:function(){
			window.history.back()
		},
		clik:function(){
			this.isa=true;
			this.isb=false;
			this.shows=true;
		},
		cliks:function(){
			this.isa=false;
			this.isb=true;
//			this.shows=false;
		}
	}

});
getData();